<?php
class documentEditAction extends sfActions
{
	public function execute($request)
	{
		$this->document_id = $request->getParameter('document_id');
		if ($this->document_id) {
			$this->document = Doctrine_Core::getTable('VTNSDocument')->find($this->document_id);
			if ($this->document)
				$this->document_form = new AdminDocumentForm($this->document);
		}
        else {
            $this->document_form = new AdminDocumentForm();
        }
		if ($request->hasParameter('document')) {
			$form = $request->getParameter('document');
			$this->status = $this->updatedocumentFromRequest($this->document, $form);
			try {
                  // get attribute of file   
	            foreach($this->request->getFiles() as $file) {
                    $source = $file['file']['tmp_name'];
                    $fileName = $file['file']['name'];
                    $fileSize = $file['file']['size'];
                    $fileType = $file['file']['type'];
                  }
                  if($fileName != "") {
						if($fileSize > $this->document->file_type_uploaded['size'] || $fileSize == 0)
							$this->message = "File upload phải nhỏ hơn 2MB";
						else 
						{                      
                          $fileExt = trim(strrchr($fileName,'.'));
                          $file_name_uploadeds = $this->document->file_type_uploaded['name'];
                          $this->message = "Dạng file được upload ";
                          foreach( $file_name_uploadeds as $file_upload) {
								if( $file_upload == $fileExt ) {
									$this->status_upload_file = true;
									break;
								}
                            else {
                                 $this->status_upload_file = false;
                                 $this->message .= $file_upload;
                            }
                                
                        }
                          // upload new file
                          if($this->status_upload_file) {
                              // delete old file if exist  
								if(isset($this->document->file) && $this->document->file != null) {
									$path_files =  "uploads/document/".$this->document->file;
									if(file_exists($path_files))
										unlink($path_files);
								}  
								$uploadDir = sfConfig::get("sf_upload_dir");
								$destination = $uploadDir.'/document/'.$fileName;
								if( !move_uploaded_file($source,$destination)) 
									$this->message = "upload file không thành công";
								else {
									$this->message = "upload file thành công";
									$this->document->file = $fileName;
									$this->document->file_type = $fileExt;
									$fileSize = $fileSize/1024;
									$this->document->file_size = round($fileSize,1);
                             }
                          }
                     }
               }
			} catch (sfException $e) {
				echo $e->getMessage();
                
			}
			
			if ($this->status) {
				$this->document->save();
                return SfView::SUCCESS;
			} else
				return SfView::SUCCESS;
		} else
			return SfView::SUCCESS;
		
	}
	
	
	private function updatedocumentFromRequest(&$document, $form)
	{
		if ($form['content'])
			$document->content = $form['content'];
		if ($form['name'])
			$document->name = $form['name'];	
		if (isset($form['is_active']))
			$document->is_active = 1;
		else
			$document->is_active = 0;
		if ($form['description'])
			$document->description = $form['description'];

		$document->updated_at = date(DATE_ISO8601);
		return true;
	}
}
?>